package jp.sourceforge.akjrcp.ui;

import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.util.FTPInfo;

import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;



public  class FTPInfoEditor extends FieldEditor {

	private Button newButton;
	private Button editButton;
	private Button removeButton;
	private TableViewer table;
	private List ftpInfoList;

	private Label label;
	private Composite menus;
	public FTPInfoEditor(String key,String description,Composite parent){
		super(key,description,parent);
		
	}
	protected void adjustForNumColumns(int numColumns) {
		//adjust
		//log.info("adjust:"+numColumns);
	}

	private Table getTable(Composite parent){
		if(table==null){
		ftpInfoList=new ArrayList();
		table=new TableViewer(parent);
		table.setLabelProvider(new ViewLabelProvider());
		table.setContentProvider(new ViewContentProvider());
		table.setInput(ftpInfoList);
		}
		return table.getTable();
	}
	
	protected void doFillIntoGrid(Composite parent, int numColumns) {
		Label label=getLabel(parent);
		GridData ld=new GridData();
		ld.horizontalSpan=numColumns;
		label.setLayoutData(ld);
		
		
		Table table=getTable(parent);
		GridData gd=new GridData(GridData.FILL_BOTH);
		//log.inf("ocol:"+numColumns);
		gd.horizontalSpan=numColumns-1;
		gd.minimumHeight=100;
		table.setLayoutData(gd); //should re-set new gridData
		
		Composite menus=getMenus(parent);
		menus.setLayoutData(new GridData());//should re-set new gridData
		
	}
	private Label getLabel(Composite parent){
		if(label==null){
			label=new Label(parent,SWT.NULL);
			label.setText(getLabelText());
			
		}
		return label;
	}

	private Composite getMenus(Composite parent){
		if(menus==null){
		menus = new Composite(parent,SWT.NULL);
		
		
		menus.setLayout(new GridLayout());
		
		
		
		newButton = createButton(menus,Messages.getString("ftpInfo.new"));
		
		editButton = createButton(menus,Messages.getString("ftpInfo.edit"));
		removeButton = createButton(menus,Messages.getString("ftpInfo.remove"));
		}
		return menus;
	}
	public Button createButton(Composite p,String label){
		Button bt=new Button(p,SWT.NULL);
		bt.setText(label);
		bt.addSelectionListener(new ButtonSelect());
		bt.setLayoutData(new GridData());
		return bt;
	}
	public class ButtonSelect implements SelectionListener{

		public void widgetSelected(SelectionEvent e) {
			if(e.widget==newButton){
				FTPInfoDialog dialog=new FTPInfoDialog(getPage().getShell());
				int bt=dialog.open();
		//		log.info(bt);
				if(bt==0){
				FTPInfo info=dialog.getFtpInfo();
				
				ftpInfoList.add(info);
				table.refresh();
				
				saveStore();
				}
				
			}else if(e.widget==editButton){
				int index=table.getTable().getSelectionIndex();
				FTPInfo info=(FTPInfo) ftpInfoList.get(index);
				FTPInfoDialog dialog=new FTPInfoDialog(getPage().getShell());
				dialog.setFtpInfo(info);
				int bt=dialog.open();
			//	log.info(bt);
				if(bt==0){
				info.copyValue(dialog.getFtpInfo());
				
				table.refresh();
				saveStore();
				}
				
			}else if(e.widget==removeButton){
				int index=table.getTable().getSelectionIndex();
				ftpInfoList.remove(ftpInfoList.get(index));
				table.refresh();
				saveStore();
			}else if(e.widget==table.getTable()){
				//should support double click
			}
		}

		public void widgetDefaultSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	/*
	private String toStoreString(List list){
		StringBuffer buffer=new StringBuffer();
		FTPInfo infos[]=(FTPInfo[]) list.toArray(new FTPInfo[0]);
		log.info(infos.length);
		log.info(table.getInput());
		for (int i = 0; i < infos.length; i++) {
			buffer.append(toStoreString(infos[i])+"\n");
		}
		return buffer.toString();
	}*/
	public static  FTPInfo[] toFTPInfos(String lines){
		if(lines.length()==0){
			return new FTPInfo[0];
		}
		String line[]=lines.split("\n");
		FTPInfo[] infos=new FTPInfo[line.length];
		for (int i = 0; i < infos.length; i++) {
			infos[i]=toFTPInfo(line[i]);
		}
		return infos;
	}
	
	private String toStoreString(FTPInfo info){
		String str="";
		str+=info.getName()+"\t"+info.getHost()+"\t"+info.getUser()+"\t"+info.getPassword()+"\t"+info.getDir()+"\t"+info.getPort();
		return str;
	}
	private static  FTPInfo toFTPInfo(String text){
		FTPInfo info=new FTPInfo();
		String values[]=text.split("\t");
		
		for (int i = 0; i < values.length; i++) {
			if(i==0){
				info.setName(values[i]);
			}else if(i==1){
				info.setHost(values[i]);
			}else if(i==2){
				info.setUser(values[i]);
			}else if(i==3){
				info.setPassword(values[i]);
			}else if(i==4){
				info.setDir(values[i]);
			}else if(i==5){
				if(values[i].length()>0){
				try{
					info.setPort(Integer.parseInt(values[i]));
				}catch(Exception e){
					//
					
				}
					
				}
			}
		}
		return info;
	}

	protected void doLoad() {
		FTPInfo infos[]=toFTPInfos(getPreferenceStore().getString(getPreferenceName()));
		for (int i = 0; i < infos.length; i++) {
			ftpInfoList.add(infos[i]);
		}
		table.refresh();
	}

	protected void doLoadDefault() {
		//do nothing.
	}

	private void saveStore(){
		setPresentsDefaultValue(false);
		
		StringBuffer buffer=new StringBuffer();
		FTPInfo infos[]=(FTPInfo[]) ftpInfoList.toArray(new FTPInfo[0]);
		for (int i = 0; i < infos.length; i++) {
			buffer.append(toStoreString(infos[i])+"\n");
		}
		getPreferenceStore().setValue(getPreferenceName(),buffer.toString());
	}
	protected void doStore() {
		//do nothing. because automatic save
	}

	public int getNumberOfControls() {
		return 2;
	}
	

	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			
			return ((FTPInfo)obj).toLabel();
		}
		public Image getColumnImage(Object obj, int index) {
			return null;
		}

	}
	
    class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
			
		}
		public void dispose() {
		}
		public Object[] getElements(Object parent) {
			
			return ((List)parent).toArray();
		}
	}

	



}

